function [ combined_decision_label, speed_vector ] = decision2label( data, frames, decision, labels )
%DECISION2LABEL Summary of this function goes here
%   Detailed explanation goes here

start_sample = 1;
cur_sample = 0;
cur_class = 0;
sections = zeros(3, 0);
for i=1:size(frames, 2)
    if decision(i) == cur_class
        cur_sample = frames(2, i);
    else
        s = [start_sample; cur_sample; cur_class];
        if cur_class
            sections = [sections s];
        end
        cur_class = decision(i);
        start_sample = frames(1, i);
    end
end

% resolve interleaves
flag = 1;
while flag
    flag = 0;
    for i=1:size(sections, 2)-1
        if sections(2, i) >= sections(1, i+1)
            flag = 1;
            if sections(2, i)-sections(1, i) > sections(2, i+1)-sections(1, i+1)
                sections(1, i+1) = sections(2, i)+1;
            else
                sections(2, i) = sections(1, i+1)-1;
            end
        end
    end
    % eliminate empty
    new_sections = zeros(3, 0);
    for i=1:size(sections, 2)
        if sections(2, i) > sections(1, i)
            new_sections(:, end+1) = sections(:, i);
        end
    end
    sections = new_sections;
end

combined_decision_label = [data(sections(1, :), 1)'; data(sections(2, :), 1)'; sections(3, :)];
%combined_decision_label = sections;

speed_vector = {};
for i=1:size(sections, 2)
    speed_vector{i} = [];
    class_name = labels{sections(3, i)}.class;
    if ~any(strcmp(class_name, {'run', 'climb_up', 'climb_down', 'walk'}))
        continue;
    end
    t = [sections(1,i) : 160 : sections(2, i)];
    speed_vector{i} = zeros(length(t), 2);
    for j=1:length(t)
        fid = find(frames(2, :) > t(j), 1, 'first');
        frame_length = data(frames(2, fid), 1) - data(frames(1, fid), 1);
        step_per_sec = 4 / frame_length;
        speed_vector{i}(j, 1) = data(t(j), 1);
        speed_vector{i}(j, 2) = step_per_sec;
    end
end

end

